<template>
  <div class="users">
    <div style="margin-top: 15px">
      <el-input
        placeholder="请输入内容"
        v-model="input"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="search"
        ></el-button>
      </el-input>
      <el-button type="primary" style="margin-left: 15px" @click="add"
        >添加用户</el-button
      >
    </div>

    <!-- <el-table
      ref="singleTable"
      :data="tableData"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column type="index" width="50"> </el-table-column>
      <el-table-column property="username" label="姓名"> </el-table-column>
      <el-table-column property="email" label="邮箱"> </el-table-column>
      <el-table-column property="mobile" label="电话"> </el-table-column>
      <el-table-column property="role_name" label="角色"> </el-table-column>
      <el-table-column property="mg_state" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @click.native="chstatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
          <el-button size="mini" type="primary" @click="editRights(scope.row)"
            >权限</el-button
          >
        </template>
      </el-table-column>
    </el-table> -->
    <MyTable
      :options="options"
      :data="tableData"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current_page="pagenum"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
      <template v-slot:status="{ row }">
        <el-switch
          v-model="row.mg_state"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @click.native="chstatus(row)"
        >
        </el-switch>
      </template>
      <template v-slot:action="{ row }">
        <el-button size="mini" @click="handleEdit(row)">编辑</el-button>
        <el-button size="mini" type="danger" @click="handleDelete(row)"
          >删除</el-button
        >
        <el-button size="mini" type="primary" @click="editRights(row)"
          >权限</el-button
        >
      </template>
    </MyTable>
    <!-- 添加编辑弹出框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="title == '添加'">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="ruleForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 分页 -->
    <!-- <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination> -->

    <!-- 权限弹出框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible3" width="60%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="当前权限">
          <el-input v-model="form.role_name"></el-input>
        </el-form-item>
        <el-form-item label="选择权限">
          <el-select v-model="qxeditID" placeholder="请选择">
            <el-option
              v-for="item in qxeditList"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="qxok()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUsers, usersAdd, del, userId, edit, editqx,getRoles } from "../utils/api";
import MyTable from "@/components/table/index.vue";
export default {
  props: {},
  data() {
    return {
      options: [
        { label: "#", type: "index" },
        { label: "姓名", property: "username" },
        { label: "邮箱", property: "email" },
        { label: "电话", property: "mobile" },
        { label: "角色", property: "role_name" },
        { label: "状态", slot: "status" },
        { label: "操作", slot: "action" },
      ],
      editId: "",
      input: "",
      title: "添加",
      query: "",
      pagenum: 1,
      pagesize: 2,
      tableData: [],
      dialogVisible: false,
      ruleForm: {},
      form: {
      
      },
      total: 0,
      qxeditID: "",
      qxeditList: [],
      dialogVisible3: false,
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入电话", trigger: "blur" }],
      },
    };
  },

  computed: {},
  created() {
    this.getuserslist();
  },
  mounted() {},
  watch: {},
  methods: {
    getuserslist() {
      getUsers({
        query: this.query,
        pagenum: this.pagenum,
        pagesize: this.pagesize,
      }).then((res) => {
        console.log(res);
        let arr = res.data.users;
        this.tableData = arr.filter((item) =>
          item.username.includes(this.input)
        );
        this.total = res.data.total;
        this.pagenum = res.data.pagenum;
      });
    },
    qxok() {
      this.dialogVisible3 = false;
      editqx({ id: this.form.id, uid: this.qxeditID }).then((res) => {
        console.log(res);
        
        this.getuserslist();
      });
    },

    // 切换状态
    chstatus(val) {
      userId({ uld: val.id, type: val.mg_state }).then((res) => {
        this.getuserslist();
      });
    },
    // 搜索
    search() {
      this.getuserslist();
    },
    // 修改
    handleEdit(row) {
      console.log(row);
      // console.log( row);
      this.editId = row.id;
      this.dialogVisible = true;
      this.ruleForm = row;
      this.title = "编辑";
    },
    editRights(id) {
      this.dialogVisible3 = true;
      this.form = id;

      getRoles({ id }).then((res) => {
        console.log(res);
        this.qxeditList = res.data;
      });
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pagesize = val;
      this.getuserslist();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagenum = val;
      this.getuserslist();
    },
    //删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          del({ id: row.id }).then((res) => {
            console.log(278, "删除");
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getuserslist();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 添加
    add() {
      this.dialogVisible = true;
    },
    //确认添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.editId) {
            //编辑
            edit({
              id: this.editId,
              email: this.ruleForm.email,
              mobile: this.ruleForm.mobile,
            }).then((res) => {
              console.log(res);
              this.getuserslist();
            });
          }
          usersAdd(this.ruleForm).then((res) => {
            console.log(res);
            this.dialogVisible = false;
            this.getuserslist();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  components: { MyTable },
};
</script>

<style scoped lang="scss">
.input-with-select {
  width: 300px;
}
.el-table {
  margin-top: 30px;
}
</style>
